var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var connect = require('gulp-connect');

var paths = {
  sass: ['./pages/**/*.scss','./app.scss'],
  // js: ['./www/pages/*/*.js','./www/services/*.js','./www/src/lib/chart/Chart.js','./www/src/js/app.js'],
  // html:['./www/pages/*/*.html']
};

gulp.task('serve', function() {
  connect.server({
    port: 8888
  });
  // run some headless tests with phantomjs 
  // when process exits: 
  // connect.serverClose();
});
// gulp.task('default', ['sass']); //,'js','html']);

// //操作：scss=>wxss
// gulp.task('sass', function(done) {
//   gulp.src('./app.scss')
//     .pipe(sass())
//     .on('error', sass.logError)
//     .pipe(minifyCss({
//       keepSpecialComments: 0
//     }))
//     .pipe(rename({ extname: '.wxss' }))
//     .pipe(gulp.dest('./'));

//   gulp.src('./pages/**/*.scss')
//     .pipe(sass())
//     .on('error', sass.logError)
//     .pipe(minifyCss({
//       keepSpecialComments: 0
//     }))
//     .pipe(rename({ extname: '.wxss' }))
//     .pipe(gulp.dest('./pages/'));

//   done();
// });

//操作：压缩html
// gulp.task('html', function() {
//   gulp.src('./www/pages/**/*.html')
//     .pipe(minifyHtml())
//     .pipe(gulp.dest('./www/pages/**/*.wxml'));
// });



//监听：sass,js,html
// gulp.task('watch', function() {
//   gulp.watch([paths.sass], ['sass']);
//   // gulp.watch([paths.js], ['js']);
//   // gulp.watch([paths.html], ['html']);
// });



gulp.task('install', ['git-check'], function() {
  return bower.commands.install()
    .on('log', function(data) {
      gutil.log('bower', gutil.colors.cyan(data.id), data.message);
    });
});

gulp.task('git-check', function(done) {
  if (!sh.which('git')) {
    console.log(
      '  ' + gutil.colors.red('Git is not installed.'),
      '\n  Git, the version control system, is required to download Ionic.',
      '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
      '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
    );
    process.exit(1);
  }
  done();
});
